/**********************************************************************************
 * Copyright (c) 2012 - 2012,  quick_tools, All right reserved
 * Title: com.adc.quicktools.net  ExecuteThread.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-6-26 下午2:11:57 
 *********************************************************************************/


package com.adc.quicktools.net;

import java.io.File;
import java.util.List;
import java.util.Properties;


import com.adc.quicktools.StringUtil;
import com.adc.quicktools.constants.ConstantDefine;
import com.adc.quicktools.file.FileControl;
import com.adc.quicktools.net.ftp.FtpUtil;
import com.adc.quicktools.net.http.HttpUtil;
import com.adc.quicktools.net.smb.SMBUtil;



public class ExecuteThread extends Thread {
	
	private int status=-1;
	private String returnInfo="";
	private Properties properties;
	private String type;
	private String remotePath;
	private String localPath;
	/**
	 *  构造方法
	* @param type up/down  指明是上传还是下载
	* @param properties 指定的ftp信息；
	* @param remotePath 远程路径
	* @param localPath 本地全路径
	 */
	public ExecuteThread(String type,Properties properties,String remotePath,String localPath) {
		// TODO Auto-generated constructor stub
		this.type=type;
		this.properties=properties;
		this.remotePath=remotePath;
		this.localPath=localPath;
	}
	
	/**
	 * 
	 * Description:   通过start()方法自动启动该方法；
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		boolean b=false;
		if (StringUtil.checkStringValue(type)) {
			System.out.println("Opera info: type="+type+"  ;  "+
					"remotePath:"+remotePath+"  ;  localPath"+localPath);
			if (properties!=null && 
				 ( (StringUtil.checkStringValue(properties.getProperty(ConstantDefine.FTP_Hostname_KEY) )&&
					StringUtil.checkStringValue(properties.getProperty(ConstantDefine.FTP_User_KEY) )&&
					StringUtil.checkStringValue(properties.getProperty(ConstantDefine.FTP_Pwd_KEY) ) )||
					(StringUtil.checkStringValue(properties.getProperty(ConstantDefine.SMB_Hostname_KEY) )&&
					 StringUtil.checkStringValue(properties.getProperty(ConstantDefine.SMB_User_KEY) )&&
					 StringUtil.checkStringValue(properties.getProperty(ConstantDefine.SMB_Pwd_KEY) ) ))) {
				if (ConstantDefine.FTP_UP.equals(type)) {  // ftp上传
					System.out.println(properties.getProperty(ConstantDefine.FTP_User_KEY)+":"
							+properties.getProperty(ConstantDefine.FTP_Pwd_KEY)+"@"
							+properties.getProperty(ConstantDefine.FTP_Hostname_KEY));
					if (StringUtil.checkStringValue(localPath)&&StringUtil.checkStringValue(remotePath)&&
							new File(localPath).isFile()) {
						b = FtpUtil.uploadOneFileByFTP(properties, remotePath, localPath);
					}
				} else if (ConstantDefine.FTP_DOWN.equals(type)) {  //  ftp下载
					System.out.println(properties.getProperty(ConstantDefine.FTP_User_KEY)+":"
							+properties.getProperty(ConstantDefine.FTP_Pwd_KEY)+"@"
							+properties.getProperty(ConstantDefine.FTP_Hostname_KEY));
					if (StringUtil.checkStringValue(localPath)&&StringUtil.checkStringValue(remotePath)) {
						FileControl.ensurePathExist(localPath);
						b = FtpUtil.downloadOneFileByFTP(properties, remotePath, localPath);
					}
				}else if (ConstantDefine.SMB_DOWN.equals(type)) {
					if (StringUtil.checkStringValue(localPath)&&StringUtil.checkStringValue(remotePath)) {
						FileControl.ensurePathExist(localPath);
						String smbPath=makeSmbUrl(getProperties(), remotePath);
						System.out.println("smbPath:"+smbPath);
						List<String> localList=SMBUtil.downloadBySMB(smbPath, localPath, true);
						if (localList!=null&&localList.size()>0) {
							b=true;
						}
					}
				}else if (ConstantDefine.SMB_UP.equals(type)) {
					if (StringUtil.checkStringValue(localPath)&&StringUtil.checkStringValue(remotePath)&&
							new File(localPath).isFile()) {
						String smbPath=makeSmbUrl(getProperties(), remotePath);
						System.out.println("smbPath:"+smbPath);
						List<String> remoteList=SMBUtil.uploadBySMB(smbPath, localPath,true);
						if (remoteList!=null&&remoteList.size()>0) {
							b=true;
						}
					}
				}
			}else {
				if (ConstantDefine.HTTP_DOWN.equals(type)) {
					if (StringUtil.checkStringValue(localPath)&&StringUtil.checkStringValue(remotePath)) {
//						if (!new File(localPath).isFile()) {
//							localPath=FileControl.checkDirEndWith(localPath)+new File(remotePath).getName();
//						}
						System.out.println("download by http:"+remotePath);
						b=HttpUtil.downloadByHttp(remotePath, localPath);
					}
				}else {
					returnInfo=returnInfo.concat(" ||  The HostInfo in properties is error! " );
				}
			}
			
		}else {
			returnInfo=returnInfo.concat(" ||  >>type :"+type );
		}
		if (b) {
			status=0;
			returnInfo=returnInfo.concat(" ||  execute over,result is:"+b );
		}
	}
	
	
	
	/**
	 * 
	 * Description:   拼接smb上传路径的文件地址
	 * author:  shen 
	 * @param properties   包含机器的smb用户名、密码等信息的properties
	 * @param remotePath  远程路径，不包含ip
	 * @return String
	 */
	private String makeSmbUrl(Properties properties,String remotePath){
		return SMBUtil.makeSmbUrl(properties, remotePath);
	}
	
	public String getRemotePath() {
		return remotePath;
	}

	public void setRemotePath(String remotePath) {
		this.remotePath = remotePath;
	}

	public String getLocalPath() {
		return localPath;
	}

	public void setLocalPath(String localPath) {
		this.localPath = localPath;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	/**
	 * 
	 * Description:   status  默认为-1，表示失败；成功为0
	 * author:  shen  
	 * @return int
	 */
	public int getStatus() {
		return status;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	
	/**
	 * 
	 * Description:   获取执行过程中的输出信息
	 * author:  shen  
	 * @return String
	 */
	public String getReturnInfo() {
		return returnInfo;
	}

}
